package eva.savant.business.administrador.view

import eva.savant.core.view.util.FacesUtils
import org.primefaces.event.SelectEvent

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.persistence.Column

@SessionScoped
@ManagedBean(name = 'IMBuilder')
public class IMBuilder {

  List<Field> entityLsColumn, entityLsColumnSel

  List<Field> detailLsColumn, detailLsColumnSel

  List<Field> entityLsField, entityLsFieldSel

  List<Field> detailLsField, detailLsFieldSel

  def selectedColumnE, selectedColumnD

  def selectedFieldE, selectedFieldD

  def nameEntity = 'cm.Compra'

  @PostConstruct
  public void uiClean() {

    entityLsColumnSel= new ArrayList()

    detailLsColumnSel= new ArrayList()

    entityLsFieldSel= new ArrayList()

    detailLsFieldSel= new ArrayList()

    entityLsColumn= createEntityProperty(nameEntity)

    detailLsColumn= createEntityProperty(nameEntity+'Detalle')

    entityLsField= createEntityProperty(nameEntity)

    detailLsField= createEntityProperty(nameEntity+'Detalle')
  }

  public createEntityProperty(String entity) {

    def clazz= this.class.classLoader.loadClass('eva.savant.businessobject.'+entity)

    def lsField= new ArrayList<Field>()


    for(x in clazz.getDeclaredFields() ) {

    if(x.type.name.equals('java.util.Collection')) continue

    def column= x.getAnnotation(Column.class)

    def   size= column != null? column.length() : null

    def   name= x.name.charAt(0).toUpperCase().toString() + x.name.substring(1)

    def   type= getTypeField(x.type.name) //;println 'prop: '+name+' t: '+x.type.name+' z: '+size

    lsField.add new Field(field:type,name:name,type:x.type.name,size:size)  }

    return lsField
  }

  public static getTypeField(String type) {

    if(type.equals('java.util.Date')) return 'D'

    if(type.equals('java.lang.String')) return 'I'

    if(type.equals('java.lang.Integer')) return 'I'

    if(type.equals('java.math.BigDecimal')) return 'I'

    return 'S'
  }

  public void onColumnESelect(SelectEvent event) {

    def field= (Field) event.object

    entityLsColumn.remove field

    entityLsColumnSel.add field
  }

  public void onColumnERemove(SelectEvent event) {

    def field= (Field) event.object

    entityLsColumnSel.remove field

    entityLsColumn.add field
  }

  public void onColumnDSelect(SelectEvent event) {

    def field= (Field) event.object

    detailLsColumn.remove field

    detailLsColumnSel.add field
  }

  public void onColumnDRemove(SelectEvent event) {

    def field= (Field) event.object

    detailLsColumnSel.remove field

    detailLsColumn.add field
  }

  public void onFieldESelect(SelectEvent event) {

    def field= (Field) event.object

    entityLsField.remove field

    entityLsFieldSel.add field
  }

  public void onFieldERemove(SelectEvent event) {

    def field= (Field) event.object

    entityLsFieldSel.remove field

    entityLsField.add field
  }

  public void onFieldDSelect(SelectEvent event) {

    def field= (Field) event.object

    detailLsField.remove field

    detailLsFieldSel.add field
  }

  public void onFieldDRemove(SelectEvent event) {

    def field= (Field) event.object

    detailLsFieldSel.remove field

    detailLsField.add field
  }

  public String buildFieldSel(List<Field> LsFieldSel) {

    def result= ''

    for(field in LsFieldSel) {

    def title= field.name

    def property= field.name.toLowerCase()

         if(field.field.equals('P'))

    result= result + "  <e:fieldI b='#{bean}' w='100px' p='${title}'/>\n\n"

    else if(field.field.equals('I'))

    result= result + "  <e:fieldI b='#{bean}' w='100px' p='${property}'/>\n\n"

    else if(field.field.equals('A'))

    result= result + "  <e:fieldA b='#{bean}' w='100px' p='${property}'/>\n\n"

    else if(field.field.equals('L'))

    result= result + "  <x:fieldL b='#{bean}' w='100px' t='${title}' v='#{bean.entity.${property}.id}'/>\n\n"

    else if(field.field.equals('S'))

    result= result + "  <x:fieldS b='#{bean}' w='100px' t='${title}' v='#{bean.entity.${property}}' x='#{x}' xL='#{x..nombre}'/>\n\n"

    else if(field.field.equals('D'))

    result= result + "  <e:fieldD b='#{bean}' w='100px' p='${property}'/>\n\n"

    if(field.field.equals('M'))

    result= result + "  <x:fieldM b='#{bean}' t='${title}' v='#{bean.detalle.${property}}' x='#{x}' id='materialId'/>\n\n"

    else if(field.field.equals('B'))

    result= result + "  <x:fieldB b='#{bean}' t='${title}' v1='#{bean.detalle.${property}}' v2='#{bean.detalle.${property}}' l='detailAdd' id='cantidadId'/>\n\n"

    }

    return result
  }

  public String buildColumnSel(List<Field> LsFieldSel) {

    def result= ''

    for(field in LsFieldSel) {

    def title= field.name

    def property= field.name.toLowerCase()

         if(field.field.equals('T'))

    result = result + "<z:columnT w='60px' t='${title}' v='#{x.${property}}'/>\n\n"

    else if(field.field.equals('D'))

    result = result + "<z:columnD w='70px' t='${title}' v='#{x.${property}}' x='t'/>\n\n"

    else if(field.field.equals('N'))

    result = result + "<p:column headerText='${title}' sortBy='#{x.${property}}' style='width:55px'><z:outputN v='#{x.${property}}' r='0'/></p:column>\n\n"

    else if(field.field.equals('M'))

    result = result + "<p:column headerText='${title}' sortBy='#{x.${property}.nombre}'><h:outputText value='#{x.${property}.codigo} - #{x.${property}.nombre}'/></p:column>\n\n"
    }

    return result
  }

  public String build(String title, String bean, String detailName) {

    def result ="<h:panelGroup xmlns:h='http://java.sun.com/jsf/html'\n" +
                "              xmlns:p='http://primefaces.org/ui'\n" +
                "              xmlns:z='http://savanti.com.bo/ui'\n" +
                "              xmlns:x='http://java.sun.com/jsf/composite/ui/component'\n" +
                "              xmlns:e='http://java.sun.com/jsf/composite/ui/component-entity' styleClass='${bean}' id='${bean}'><p:growl id='growl'/>\n" +
                "\n" +
                "<z:beanMenubar bean='#{${bean}}' title='${title} #{${bean}.entity.id}'/>\n" +
                "\n" +
                "<h:panelGrid columns='1' width='100%' rendered='#{${bean}.form}'>\n" +
                "\n" +
                "<h:panelGrid id='bean' columns='2' styleClass='bean align-top'>\n" +
                "\n" +
                "  <x:beanPanel id='hd' bean='#{${bean}}' title='Cabecera' cellpadding='7'>\n" +
                "\n" + buildFieldSel(entityLsFieldSel) +
                "  </x:beanPanel>\n" +
                "\n" +
                "\n" +
                "  <x:beanPanel id='ps' bean='#{${bean}}' title='Posicion' cellpadding='5' style='min-width: 500px'>\n" +
                "\n" +
                "  <h:panelGrid columns='1' cellpadding='3' style='margin-right: 22px'>\n" +
                "\n" + buildFieldSel(detailLsFieldSel) +
                "  </h:panelGrid>\n" +
                "\n" +

                "\n" +
                "  </x:beanPanel>\n" +
                "\n" +
                "</h:panelGrid>\n" +
                "\n" +
                "<x:materialTable bean='#{${bean}}' ls='#{${bean}.entity.ls${detailName}}' edit='false'>\n" +
                "\n" + buildColumnSel(detailLsColumnSel) +
                "</x:materialTable>\n" +
                "\n" +
                "</h:panelGrid>\n" +
                "\n" +
                "\n" +
                "<e:dataTable bean='#{${bean}}'>\n" +
                "\n" + buildColumnSel(entityLsColumnSel) +
                "</e:dataTable>\n\n" +
                "</h:panelGroup>"

    return result
  }

  public void uiMerge() { try {

    def module = ['cg':'Business FI-CG,contabilidad',
                  'cm':'Business MM-MT,compras',
                  'vt':'Business SD-VT,ventas']

    def split  = nameEntity.split('\\.')


    def name = split[1]

    def data = module[split[0]]

    def namePath = data.split('\\,')[1]

    def nameModule = data.split('\\,')[0]


    def bean = 'IT' + name

    def nameFile= "X:/Workspace/Savanti/${nameModule}/web/business/${namePath}/${bean}.xhtml"

    def contentFile= build(name, bean, name + 'Detalle')


    def file = new File( nameFile )

    if(file.exists()) file.delete()

    def writer= new FileWriter(file,true)

    def pw = new PrintWriter(writer)

    pw.println(contentFile)

    pw.flush()

    pw.close()

    FacesUtils.addInfoMessage "Se creo la transaccion ${bean}"

  } catch (Exception e) { FacesUtils.addErrorMessage e } }


  class Field {

    String  field

    String  name

    String  type

    Integer size
  }

}

